package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCampBuff;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCity;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCityEvent;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingDataDaoImpl extends TempDataObjectDao<CrossTongBingData> {

    public static String getCrossTongBingDataSql = "select * from `t_c_tongbing_data` where `activityId`=? and groupId=? ";

    public CrossTongBingData getCrossTongBingData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossTongBingDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossTongBingDataSql = "update `t_c_tongbing_data` set lockUnion=?,process2WinUserId=?,refreshCityEventTime=?,applyCityEventTime=?,cityEventMap=?,campBuffMap=?,npcEventAddTime=?,cityMap=?,autoSendCollect=? where activityId=? and groupId=?";

    @Override
    protected boolean updateData(CrossTongBingData crossTongBingData) {
        crossTongBingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, crossTongBingData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getRefreshCityEventTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getApplyCityEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCityEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCampBuffMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getNpcEventAddTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCityMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getAutoSendCollect()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossTongBingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossTongBingDataSql = "insert into `t_c_tongbing_data`(activityId,groupId,lockUnion,process2WinUserId,refreshCityEventTime,applyCityEventTime,cityEventMap,campBuffMap,npcEventAddTime,cityMap,autoSendCollect) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossTongBingData crossTongBingData) {
        crossTongBingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getGroupId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossTongBingData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getRefreshCityEventTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getApplyCityEventTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCityEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCampBuffMap())));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingData.getNpcEventAddTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingData.getCityMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingData.getAutoSendCollect()));
        if (getDb().executeNoneQuery(insertCrossTongBingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossTongBingData parseData(ResultSet rs) throws SQLException {
        CrossTongBingData crossTongBingData = new CrossTongBingData();
        crossTongBingData.setActivityId(rs.getInt("activityId"));
        crossTongBingData.setGroupId(rs.getInt("groupId"));
        crossTongBingData.setLockUnion(rs.getBoolean("lockUnion"));
        crossTongBingData.setProcess2WinUserId(rs.getLong("process2WinUserId"));
        crossTongBingData.setRefreshCityEventTime(rs.getLong("refreshCityEventTime"));
        crossTongBingData.setApplyCityEventTime(rs.getLong("applyCityEventTime"));
        crossTongBingData.setCityEventMap(JSON.parseObject(rs.getString("cityEventMap"), new TypeReference<ConcurrentHashMap<Integer, CrossTongBingCityEvent>>() {
        }));
        crossTongBingData.setCampBuffMap(JSON.parseObject(rs.getString("campBuffMap"), new TypeReference<ConcurrentHashMap<Integer, List<CrossTongBingCampBuff>>>() {
        }));
        crossTongBingData.setNpcEventAddTime(rs.getLong("npcEventAddTime"));
        crossTongBingData.setCityMap(JSON.parseObject(rs.getString("cityMap"), new TypeReference<ConcurrentHashMap<Integer, CrossTongBingCity>>() {
        }));
        crossTongBingData.setAutoSendCollect(rs.getInt("autoSendCollect"));
        return crossTongBingData;
    }

}
